package com.changgou.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.canal.config.RabbitMQConfig;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;


@CanalEventListener  //声明当前的类为canal的监听类
public class BusinessListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     *监听广告数据
     * @param eventType 当前操作数据库的类型
     * @param rowData   当前操作数据库的数据
     */
    @ListenPoint(schema = "changgou_business", table = "tb_ad")    //也可以监控多个表，用{}和,分割
    public void adUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.err.println("广告数据发生变化");

        //改变前的数据
        //rowData.getBeforeColumnsList().forEach((c) -> System.out.println("更改前数据: " + c.getName() + " :: " + c.getValue()));
        //改变后的数据
        //rowData.getAfterColumnsList().forEach((c) -> System.out.println("更改后数据: " + c.getName() + " :: " + c.getValue()));

        //获取改变之后的数据
        for(CanalEntry.Column column: rowData.getBeforeColumnsList()) {

            if("position".equals(column.getName())){
                System.out.println("发送最新的消息到MQ:"+column.getValue());
                //发送消息
                rabbitTemplate.convertAndSend("",RabbitMQConfig.AD_UPDATE_QUEUE,column.getValue());
            }
        }

    }
}
